package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.bo.backgroundv2.LoginLogBo;
import com.dmap.admin.controller.backgroundv2.param.LoginLogSelectParam;
import com.dmap.admin.po.LoginLog;
import com.dmap.admin.service.backgroundv2.LoginLogService;
import com.dmap.admin.vo.backgroundv2.RecordNumAndLoginLogVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 04/05/18
 * \* Description:
 * \
 */
@Api(value = "登陆日志管理", description = "background/登陆日志管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/loginLog/")
public class LoginLogController {

    @Resource
    private LoginLogService loginLogService;

    @ResponseBody
    @ApiOperation(value = "根据userId查询登陆日志列表",
            notes = "1.必填：userId;" +
                    "2.必填：pageIndex(>=1), pageSize(>=1);" +
                    "3.返回登陆日志列表;")
    @RequestMapping(value = "select/selectByUserId", method = RequestMethod.POST)
    public BaseVo selectByUserId(@RequestBody LoginLogSelectParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getUserId())
                || !StringUtils.isNumeric(param.getUserId()) || Long.valueOf(param.getUserId()).equals(0L)
                || param.getPageIndex() == null || param.getPageSize() == null
                || param.getPageIndex() < 1 || param.getPageSize() < 1) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        RecordNumAndLoginLogVo recordNumAndLoginLogVo = new RecordNumAndLoginLogVo();
        int recordNumber = loginLogService.countByUserId(Long.valueOf(param.getUserId()));
        if (recordNumber > 0) {
            List<LoginLog> list = loginLogService.selectByLoginLogSelectParam(param);
            List<LoginLogBo> voList = new ArrayList<>();
            list.forEach(loginLog ->
                    voList.add(new LoginLogBo(loginLog))
            );
            recordNumAndLoginLogVo.setRecordNumber(recordNumber);
            recordNumAndLoginLogVo.setLoginLogBoList(voList);
        }
        baseVo.setData(recordNumAndLoginLogVo);
        return baseVo;
    }

}